<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <link rel="stylesheet" type="text/css" href="../css/aui.2.0.css" />
</head>

<body>
    <header class="aui-bar aui-bar-nav" style="background-color: #03a9f4;position:fixed;" id="header">
        <a class="aui-pull-left aui-btn" tapmode onclick="closeWin();">
            <span class="aui-iconfont aui-icon-left"></span>
        </a>
        <div class="aui-title" id="title">
            总成问题录入
        </div>
    </header>
    <div class="aui-content aui-margin-b-15" style="margin-top:50px;">
        <ul class="aui-list aui-form-list">
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">计划日期</div>
                    <div class="aui-list-item-input" onclick="timePicker();">
                        <input type="text" placeholder="选择日期" class="aui-input" id="proddate">
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">生产线</div>
                    <select id="workCenterCode">
                  </select>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">班次</div>
                    <select id="shiftCode">
                  </select>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">缺陷分类</div>
                    <select id="chkItemType" onchange="onChange()">
                    </select>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">总成条码</div>
                    <div class="aui-list-item-input">
                        <input type="text" placeholder="扫描 总成条码" id="zcTm" onclick="getIdSelector();" />
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">返修批次</div>
                    <div class="aui-list-item-input">
                        <input type="text" id="lotCode" onclick="getIdSelector();">
                    </div>
                </div>
            </li>
        </ul>

        <div class='aui-btn aui-btn-info aui-btn-block' style="margin-top:10px;" onclick='save()'>保存</div>
        <div style="background-color:white; height: 100%;width: 100%;margin-top:2%;">
            <table class="table" style='width:100%;' id="table">
                <thead>
                    <tr style="text-align: center;background-color: #DCE9F2;color: #467aa7;font-size: 12px;font-weight: bold;">
                        <td style="width:30px;"></td>
                        <td algin='center' style="width:50px;">序号</td>
                        <td algin='center'>缺陷编码</td>
                        <td algin='center'>缺陷名称</td>
                        <td style="display:none;">id</td>
                    </tr>
                </thead>
                <tbody id="tbodyHtml">
                </tbody>
            </table>
        </div>
    </div>

    <div>
        <div style="height:60px;"></div>
        <footer class="aui-bar aui-bar-tab" id="footer">
            <div class="aui-bar-tab-item" tapmode id="scan" onclick="doScan()">
                <i class="aui-iconfont aui-icon-scan aui-text-epblue" style="font-size: 1.8rem"></i>
            </div>
        </footer>
    </div>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="../script/public.js"></script>
<script type="text/javascript">
    //光标所在id
    var idSelector = "";

    // 获取光标所在组件的id
    function getIdSelector() {
        var e = event || window.event;
        var el = e.srcElement || e.target;
        idSelector = el.id;
    }

    //初始化
    apiready = function() {

        //初始化当前日期
        crateDate();

        //查询下拉框--生产线，班次
        getCombox();

        //查询下拉框--缺陷分类
        getComboxChkItemType();

    }

    //获取当前日期
    function crateDate() {
        var date = new Date();
        var year = date.getFullYear();
        var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
        var day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
        $("#proddate").val(year + "-" + month + "-" + day);
    }

    //查询生产线，班次下拉框
    function getCombox() {
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getComboxLb";
        var data = {
            values: {
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function(ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");

                //成功
                if (myobj.code == "0") {

                    //生产线
                    var lineList = eval(myobj.lineList);
                    //班次
                    var shiftList = eval(myobj.shiftList);

                    createCombox(lineList, "workCenterCode");
                    createCombox(shiftList, "shiftCode");

                } else {
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            } else {
                alert(JSON.stringify(err));
            }
        });
    }

    //质量类型下拉框
    function getComboxChkItemType() {
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getUtilComboxList";
        var data = {
            values: {
                tablename: "uqc_bd_chk_item_type",
                column_name: "name",
                column_value: "sysid",
                condition: "is_delete=0 ",
                un:un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function(ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var map = eval("(" + data + ")");
                var list = eval(map.list);
                var html = "";
                for (var i = 0; i < list.length; i++) {
                    html += "<option value='" + list[i].value + "'>" + list[i].label + "</option>";
                }
                $api.html($api.byId("chkItemType"), html);
            }
        });
    }

    //构造下拉框
    function createCombox(list, id) {

        var html = "<option value=''>--请选择--</option>";
        for (var i = 0; i < list.length; i++) {
            html += "<option value='" + list[i].code + "'>" + list[i].name + "</option>";
        }
        $api.html($api.byId(id), html);
    }

    //时间选择器
    function timePicker() {
        api.openPicker({
            type: 'date',
            date: new Date(),
            title: '选择日期'
        }, function(ret, err) {
            if (ret) {
                var monthTemp = (ret.month < 10 ? '0' + ret.month : ret.month);
                var dayTemp = (ret.day < 10 ? '0' + ret.day : ret.day);
                var date = ret.year + "-" + monthTemp + "-" + dayTemp;
                $("#proddate").val(date);
            }
        });
    }

    //扫描
    function doScan() {
        var moduleScanner = api.require('moduleScanner');
        var params = {
            top: 150, //扫描窗口的上边距
            width: 250, //扫描窗口的宽
            height: 250, //扫描窗口的高
            useColor: false, //是否使用自己设定的颜色(startColor和endColor),
            //如果不使用设定的颜色就使用默认的那张网格图
            startColor: '#0F08C8FF', //渐变起始颜色
            endColor: '#7F08C8FF', //渐变结束颜色
            animTime: 2200, //扫描动画时间,单位为毫秒
        };
        moduleScanner.toScannerActivityForResult(params, function(ret, err) {

            $("#" + idSelector).val(ret);

        });
    }

    //缺陷分类值改变--查询质量原因
    function onChange() {
        //缺陷分类
        var chkItemType = $("#chkItemType").val();

        //计划日期非空校验
        if (chkItemType == null || chkItemType == "" || chkItemType == undefined) {

            $api.html($api.byId("tbodyHtml"), "");
            return;

        } else {

            var un = localStorage.getItem('un');
            var serverUrl = localStorage.getItem('serverUrl');
            var url = serverUrl + "/app/doSearchQualChkItem";
            var data = {
                values: {
                    chkItemType: chkItemType,
                    un: un
                }
            };
            api.ajax({
                url: url,
                method: 'post',
                dataType: 'json',
                timeout: 300,
                returnAll: false,
                data: data
            }, function(ret, err) {
                if (ret) {
                    var data = JSON.stringify(ret);
                    var myobj = eval("(" + data + ")");

                    //返回成功
                    if (myobj.code == "0") {
                        var list = eval(myobj.list);
                        var html = "";
                        for (var i = 0; i < list.length; i++) {

                            //只可以修改未退货的数据
                            html += "<tr style='text-align: center;color: black;font-size: 10px;background-color: #F5F5F5;'>";
                            html += "<td><input name='sub' type='checkbox' /></td>";
                            html += "<td name='seqno'>" + (i + 1) + "</td>";
                            html += "<td name='code'>" + list[i].code + "</td>";
                            html += "<td name='name'>" + list[i].name + "</td>";
                            html += "<td name='id' style='display:none;'>" + list[i].id + "</td>";
                            html += "</tr>";
                        }
                        $api.html($api.byId("tbodyHtml"), html);

                        //失败
                    } else if (myobj.code == "1") {

                        //清空表格数据
                        $api.html($api.byId("tbodyHtml"), "");
                        api.toast({
                            msg: myobj.message,
                            duration: 3000
                        });
                    }
                } else {
                    alert(JSON.stringify(err));
                }
            });
        }
    }

    //总成条码扫描回车
    $("#zcTm").on('keypress', function(e) {
        var keycode = e.keyCode;
        var tm = $(this).val();
        if (keycode == '13') {
            $("#zcTm").val(tm);
        }
    });

    //批次号扫描回车
    $("#lotCode").on('keypress', function(e) {
        var keycode = e.keyCode;
        var tm = $(this).val();
        if (keycode == '13') {
            $("#lotCode").val(tm);
        }
    });

    //保存
    function save() {

        //计划日期
        var proddate = $("#proddate").val();
        //生产线
        var workCenterCode = $("#workCenterCode").val();
        //班次
        var shiftCode = $("#shiftCode").val();
        //缺陷分类
        var chkItemType = $("#chkItemType").val();
        //总成条码
        var zcTm = $("#zcTm").val();
        //返修批次
        var lotCode = $("#lotCode").val();

        //计划日期非空校验
        if (proddate == null || proddate == "" || proddate == undefined) {
            api.toast({
                msg: '请选择计划日期！',
                duration: 3000
            });
            return;
        }

        //生产线非空校验
        if (workCenterCode == null || workCenterCode == "" || workCenterCode == undefined) {
            api.toast({
                msg: '请选择生产线！',
                duration: 3000
            });
            return;
        }

        //班次非空校验
        if (shiftCode == null || shiftCode == "" || shiftCode == undefined) {
            api.toast({
                msg: '请选择班次！',
                duration: 3000
            });
            return;
        }

        //缺陷分类非空校验
        if (chkItemType == null || chkItemType == "" || chkItemType == undefined) {
            api.toast({
                msg: '请选择缺陷分类！',
                duration: 3000
            });
            return;
        }

        //总成条码非空校验
        if (zcTm == null || zcTm == "" || zcTm == undefined) {
            api.toast({
                msg: '请扫描/录入 总成条码！',
                duration: 3000
            });
            return;
        }

        //选中数据
        var check = $("input[type='checkbox']:checked"); //在table中找input下类型为checkbox属性为选中状态的数据
        var count = 0;
        check.each(function() {
            count++;
        });

        //选中行不为1
        if (count < 1) {
            api.toast({
                msg: '请至少选择一条质量原因！',
                duration: 3000
            });
            return;
        }

        //质量原因
        var ids = "";

        //遍历
        check.each(function() {
            var row = $(this).parent("td").parent("tr");
            ids += row.find("[name='id']").html() + ",";
        });

        if (ids.length > 0) {
            ids = ids.substring(0, ids.length - 1);
        }

        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/saveZcChkItemType";
        var data = {
            values: {
                proddate: proddate,
                workCenterCode: workCenterCode,
                shiftCode: shiftCode,
                chkItemType: chkItemType,
                zcTm: zcTm,
                lotCode: lotCode,
                ids: ids,
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function(ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");

                //返回成功
                if (myobj.code == "0") {

                    api.toast({
                        msg: "保存成功",
                        duration: 3000
                    });

                    $("#zcTm").val("");
                    $("#lotCode").val("");
                    onChange();

                    //失败
                } else if (myobj.code == "1") {

                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            } else {
                alert(JSON.stringify(err));
            }
        });
    }

    //返回按钮
    function closeWin() {
        api.closeWin({});
    }
</script>

</html>
